package com.xdap.common.dao;

import com.definesys.mpaas.query.MpaasQuery;
import com.xdap.common.pojo.dto.webhookTest.XdapProcInstance;
import com.xdap.fsapproval.module.pojo.ApaasProcNotificationLog;
import com.xdap.fsapproval.module.pojo.XdapApproveHistory;
import com.xdap.fsapproval.module.pojo.XdapMenus;
import com.xdap.fsapproval.module.pojo.XdpFeiShuConfigs;
import com.xdap.fsapproval.pojo.ApaasApplications;
import com.xdap.runtime.service.RuntimeDatasourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @BelongsProject：IntelliJ IDEA
 * @BelongsPackage：com.adisseo.service.approval.dao
 * @Author：fei
 * @CreateTime：2022-09-06 18:43
 * @Description: 审批查询
 * @Version：1.0
 */
@Repository
@RequiredArgsConstructor
public class ApproveDao {

    public static final String XDAP_MYSQL_PREFIX = "xdap_app_";
    public static final String XDAP_ACTIVITI_PREFIX = "xdap_activiti_";
    private final RuntimeDatasourceService runtimeDatasourceService;

    private MpaasQuery buildQuery() {
        return runtimeDatasourceService.buildTenantNoSchemaMpaasQuery();
    }

    private MpaasQuery buildActivitiQuery(String tenantId) {
        return runtimeDatasourceService.buildTenantMpaasQuery(XDAP_ACTIVITI_PREFIX + tenantId);
    }

    public List<XdapApproveHistory> listXdapApproveHistory(String instanceId, String tenantId, String tableName) {
        return buildActivitiQuery(tenantId)
                .eq("instance_id", instanceId)
                .table(XDAP_ACTIVITI_PREFIX + tenantId + "." + tableName)
                .orderBy("approve_date", "asc")
                .doQuery(XdapApproveHistory.class);
    }

    public XdapMenus selectTitle(String menuId, String tenantId) {
        return buildQuery()
                .eq("id", menuId)
                .table(XDAP_MYSQL_PREFIX + tenantId + ".xdap_menus")
                .doQueryFirst(XdapMenus.class);
    }

    public XdpFeiShuConfigs selectId(String appId, String tenantId) {
        return buildQuery()
                .eq("app_id", appId)
                .table(XDAP_MYSQL_PREFIX + tenantId + ".xdap_fei_shu_configs")
                .doQueryFirst(XdpFeiShuConfigs.class);
    }

    public ApaasApplications selectUrl(String id, String tenantId) {
        return buildQuery()
                .eq("id", id)
                .table(XDAP_MYSQL_PREFIX + tenantId + ".apaas_applications")
                .doQueryFirst(ApaasApplications.class);
    }

    public List<ApaasProcNotificationLog> selectApaasProcNotificationLog(String documentId, String tenantId) {
        return buildActivitiQuery(tenantId)
                .eq("document_id", documentId)
                .table(XDAP_ACTIVITI_PREFIX + tenantId + ".apaas_proc_notification_log")
                .doQuery(ApaasProcNotificationLog.class);
    }

    public XdapMenus selectMenusId(String formId, String tenantId) {
        return buildQuery()
                .eq("formId", formId)
                .table(XDAP_MYSQL_PREFIX + tenantId + ".xdap_menus")
                .doQueryFirst(XdapMenus.class);
    }
    public XdapProcInstance getXdapProcInstance(String documentId, String tenantId) {
        return buildActivitiQuery(tenantId)
                .eq("document_id", documentId)
                .table(XDAP_ACTIVITI_PREFIX + tenantId + ".xdap_proc_instance")
                .doQueryFirst(XdapProcInstance.class);
    }
}
